package pl.hexar.action;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;

import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.framework.EntityHome;

import pl.hexar.model.TmpUser;

@Scope(ScopeType.CONVERSATION)
@Name("tmpUserHome")
public class TmpUserHome extends EntityHome<TmpUser> {

	@RequestParameter
	Long login;

	@In
	private AsynchronousMailProcessor asynchronousMailProcessor;

	@In()
	EntityManager entityManager;

	@In(create = true)
	@Out(scope=ScopeType.CONVERSATION)
	private PasswordBean passwordBean;

	private String activationCode;

	private boolean enabled = true;

	public boolean getEnabled() {
		return enabled;
	}

	public void switchEnabled() {
		this.enabled = !this.enabled;
	}

	public void setDisabled(boolean enabled) {
		this.enabled = enabled;
	}


	public String getActivationCode() {
		return activationCode;
	}

	public void setActivationCode(String name) {
		this.activationCode = HexarUtil.codeString(name);
	}

	@Override
	public Object getId() {
		if (login == null) {
			return super.getId();
		} else {
			return login;
		}
	}
	@Override
	@Transactional()
	public String persist() {

		getInstance().setPassword(passwordBean.getPassword());
		getInstance().setRegDate(new Date());
		this.setActivationCode(getInstance().getLogin());
		asynchronousMailProcessor.scheduleSend(this);

		return super.persist();
	}


	@Override
	@Begin()
	public void create() {
		super.create();
	}

	public void validateLogin(FacesContext ctx, UIComponent comp, Object obj)
			throws ValidatorException {
		if (loginUsed(obj.toString()) || tmpLoginUsed(obj.toString())) {
			FacesMessage message = new FacesMessage();
			message.setDetail("Login in use " + obj);
			// message.setSummary("The page size has to be a valid number");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}

	}

	public void validateEmail(FacesContext ctx, UIComponent comp, Object obj)
			throws ValidatorException {
		if (emailUsed(obj.toString()) || emailTmpUsed(obj.toString())) {
			FacesMessage message = new FacesMessage();
			message.setDetail("Email in use " + obj);
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);

		}
	}

	private boolean emailTmpUsed(String email) {
		Query findTmpUser = entityManager
				.createNamedQuery("TmpUser.findByEmail");
		findTmpUser.setParameter("email", email);
		try {
			findTmpUser.getSingleResult();
		} catch (NoResultException nre) {
			return false;
		}

		return true;
	}

	private boolean emailUsed(String email) {
		Query findUser = entityManager.createNamedQuery("User.findByEmail");
		findUser.setParameter("email", email);
		try {
			findUser.getSingleResult();
		} catch (NoResultException nre) {
			return false;
		}
		return true;
	}

	boolean loginUsed(String name) {

		Query findUser = entityManager.createNamedQuery("User.findByLogin");
		findUser.setParameter("login", name);
		try {
			findUser.getSingleResult();
		} catch (NoResultException nre) {
			return false;
		}

		return true;
	}

	boolean tmpLoginUsed(String name) {
		Query findTmpUser = entityManager
				.createNamedQuery("TmpUser.findByLogin");
		findTmpUser.setParameter("login", name);
		try {
			findTmpUser.getSingleResult();
		} catch (NoResultException nre) {
			return false;
		}

		return true;
	}

}
